package com.xty.itgroup.tech05tytex.Controller;

import com.alibaba.fastjson.JSONObject;
import com.xty.itgroup.tech05tytex.Databean.Bean01YarnTag;
import com.xty.itgroup.tech05tytex.Databean.Bean02ClothTags;
import com.xty.itgroup.tech05tytex.Logic.Logic01Base;
import com.xty.itgroup.tech05tytex.Util.Result;
import com.xty.itgroup.tech05tytex.Util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@Api(value = "BaseController", tags = "基础资料接口")
@RestController
@RequestMapping("/base")
public class BaseController {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private Logic01Base base;
    private static Logger log= LoggerFactory.getLogger(BaseController.class);
    @ApiOperation(value = "测试接口", notes = "接通测试")
    @RequestMapping(value = "test",method = RequestMethod.GET)
    public Result test(){
        return ResultUtil.success();
    }
//    @ApiOperation(value = "基础纱线资料", notes = "基础纱线资料")
//    @RequestMapping(value = "/baseyarn",method = RequestMethod.GET)
//    public Result baseYarn(String start_time,String end_time){
//        JSONObject yarns = restTemplate.getForObject("http://sinotytex.com:9999/data/getbomyarn", JSONObject.class);
//        List<Map<String, Object>> list= (List<Map<String, Object>>) yarns.get("data");
//        base.addYarns(list);
//        return ResultUtil.success();
//    }
    @ApiOperation(value = "坯布工艺资料", notes = "坯布工艺资料")
    @RequestMapping(value = "/basegreybom",method = RequestMethod.GET)
    public Result baseGreyBom(){
        JSONObject boms = restTemplate.getForObject("http://sinotytex.com:9999/data/getgreybom",
                JSONObject.class);
        List<Map<String,Object>> list= (List<Map<String, Object>>) boms.get("data");
        base.addGreyBomDetail(list);
        JSONObject bomMain = restTemplate.getForObject("http://sinotytex.com:9999/data/getgreybommain",JSONObject.class);
        list= (List<Map<String, Object>>) bomMain.get("data");
        base.addGreyBomMain(list);
        return ResultUtil.success();
    }
    @ApiOperation(value = "基础纱线资料", notes = "基础纱线资料")
    @RequestMapping(value = "/baseyarnnew",method = RequestMethod.GET)
    public Result baseYarnNew(){
        JSONObject yarns = restTemplate.getForObject("http://sinotytex.com:9999/data/getbomyarn", JSONObject.class);
        List<Map<String,Object>> list= (List<Map<String,Object>>) yarns.get("data");
        base.addYarnsNew(list);
        return ResultUtil.success();
    }
    @ApiOperation(value = "查询纱线资料", notes = "根据纱线名称查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "yarn_name", value = "纱线名称(空格分开)", required = true, dataType = "string"),
            @ApiImplicitParam(name = "type", value = "-1-全部,0-无,1-有", required = true, dataType = "int",example = "-1")
    })
    @RequestMapping(value = "/listyarns",method =RequestMethod.GET )
    public Result listYarns(String yarn_name,int type){
        return ResultUtil.success(base.listYarns(yarn_name,type));
    }
    @ApiOperation(value = "添加标签", notes = "添加标签")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tag_name", value = "标签名", required = true, dataType = "string"),
            @ApiImplicitParam(name = "parent_id", value = "上层编号", required = true, dataType = "int" ,example = "1"),
            @ApiImplicitParam(name = "tag_unit", value = "单标签表示价格", required = true, dataType = "double" ,example = "1.0"),
            @ApiImplicitParam(name = "type", value = "类别1-纱线/2-织造", required = true, dataType = "int" ,example = "1")
    })
    @RequestMapping(value = "/addtag",method = RequestMethod.POST)
    public Result addYarnTag(String tag_name,int parent_id,double tag_unit,int type){
        base.addYarnTag(tag_name, parent_id, tag_unit,type);
        return ResultUtil.success();
    }
    @ApiOperation(value = "修改标签", notes = "删除+重新添加标签")
    @RequestMapping(value = "/updatetag",method = RequestMethod.POST)
    public Result updateYarnTag(@RequestBody Bean01YarnTag tag){
        base.updateYarnTag(tag);
        return ResultUtil.success();
    }
    @ApiOperation(value = "分组获取所有标签", notes = "查询并分组")
    @RequestMapping(value = "/listtag",method = RequestMethod.GET)
    public Result listYarnTag(){
        return ResultUtil.success(base.listTags());
    }
    @ApiOperation(value = "树形获取所有标签", notes = "树形查询")
    @RequestMapping(value = "/listtreetag",method = RequestMethod.GET)
    public Result listYarnTreeTag(){
        return ResultUtil.success(base.listTreeTags());
    }
    @ApiOperation(value = "纱线-标签绑定", notes = "重复需要先解绑")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tag_ids", value = "标签集", required = true, dataType = "string"),
            @ApiImplicitParam(name = "yarn_ids", value = "纱线集", required = true, dataType = "string"),
            @ApiImplicitParam(name = "yarn_price", value = "纱线每公斤设定单价", required = true, dataType = "double",example = "1.0"),
            @ApiImplicitParam(name = "flag", value = "0-非强制/1-强制", required = true, dataType = "int",example = "0")

    })
    @RequestMapping(value = "/addyarntag",method = RequestMethod.POST)
    public Result addYarnTags(String tag_ids,String yarn_ids,double yarn_price,int flag){
        base.addYarnTags(tag_ids, yarn_ids, yarn_price, flag);
        return ResultUtil.success();
    }
    @ApiOperation(value = "修改纱线价格(解绑+绑定新的关系)", notes = "删除+新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "int",example = "1"),
            @ApiImplicitParam(name = "yarn_unit", value = "纱线价格", required = true, dataType = "double",example = "1.0"),
            @ApiImplicitParam(name = "tag_ids", value = "标签集", required = true, dataType = "string")
    })
    @RequestMapping(value = "/update1yarntag",method =RequestMethod.POST )
    public Result update1YarnTag(int id,String tag_ids,double yarn_unit){
        base.update1YarnTag(id, tag_ids, yarn_unit);
        return ResultUtil.success();
    }
    //查询
    @ApiOperation(value = "查询布列表", notes = "品名模糊查询")
    @ApiImplicitParam(name = "product_name", value = "品名", required = false, dataType = "string")
    @RequestMapping(value = "/listproduct",method =RequestMethod.GET )
    public Result listProduct(String product_name){
        return ResultUtil.success(base.listProduct(product_name));
    }
    @ApiOperation(value = "获取价格", notes = "根据品名精确查询")
    @ApiImplicitParam(name = "product_name", value = "品名", required = true, dataType = "string")
    @RequestMapping(value = "/searchunit",method =RequestMethod.GET )
    public Result searchProductUnit(String product_name){
        return ResultUtil.success(base.searchClothUnit(product_name));
    }
    @ApiOperation(value = "生产属性绑定", notes = "添加布-生产属性绑定")
    @RequestMapping(value = "/addclothtag",method =RequestMethod.POST )
    public Result addClothTags(@RequestBody Bean02ClothTags tag){
        base.add1WeaveTag(tag);
        return ResultUtil.success();
    }
    @ApiOperation(value = "纱线价格列表", notes = "纱线名称模糊查询")
    @ApiImplicitParam(name = "yarn_name", value = "纱线名称", required = false, dataType = "string")
    @RequestMapping(value = "/listyarnunit",method =RequestMethod.GET )
    public Result listYarnUnit(String yarn_name){
        return ResultUtil.success(base.listYarnsWithUnit(yarn_name));
    }
    @ApiOperation(value = "产品价格列表", notes = "布品名模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "flag", value = "-1全部/1-删除/0-正常", required = true, dataType = "int",example =
                    "-1"),
            @ApiImplicitParam(name = "product_name", value = "品名", required = false, dataType = "string")
    })
    @RequestMapping(value = "/listclothunit",method =RequestMethod.GET )
    public Result listClothUnit(String product_name,int flag){
        return ResultUtil.success(base.listClothWithUnit(product_name,flag));
    }
    @ApiOperation(value = "产品价格列表1", notes = "布品名精确查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "flag", value = "-1全部/1-删除/0-正常", required = true, dataType = "int",example =
                    "-1"),
            @ApiImplicitParam(name = "product_name", value = "品名", required = true, dataType = "string")
    })

    @RequestMapping(value = "/listclothunit1",method =RequestMethod.GET )
    public Result listClothUnit1(String product_name,int flag){
        return ResultUtil.success(base.listClothWithUnit1(product_name,flag));
    }
}
